package bg.btanev.eightyfour.client.composite;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.BeforeSelectionEvent;
import com.google.gwt.event.logical.shared.BeforeSelectionHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedTabBar;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class BasicTabComposite extends Composite {

	private final DecoratedTabBar actionsMenuTabBar;
	private final VerticalPanel actionDataCenterPanel;
	private final VerticalPanel dataPanel;

	private String tabIdentifier;
	private String tabCaption;

	public BasicTabComposite() {
		final VerticalPanel containerPanel = new VerticalPanel();
		initWidget(containerPanel);
		containerPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		containerPanel.setWidth("100%");

		final DockPanel actionsPanel = new DockPanel();
		actionsPanel.setBorderWidth(1);
		actionsPanel.setWidth("100%");
		actionsPanel.setVisible(false);

		actionsMenuTabBar = new DecoratedTabBar();
		actionsMenuTabBar.setWidth("100%");
		actionsMenuTabBar.addBeforeSelectionHandler(new BeforeSelectionHandler<Integer>() {
			@Override
			public void onBeforeSelection(BeforeSelectionEvent<Integer> event) {
				if (event.getItem().intValue() >= 0) {
					actionsPanel.setVisible(true);
				}
			}
		});

		containerPanel.add(actionsMenuTabBar);
		containerPanel.add(actionsPanel);

		actionDataCenterPanel = new VerticalPanel();
		actionsPanel.add(actionDataCenterPanel, DockPanel.CENTER);
		actionDataCenterPanel.setWidth("100%");

		HorizontalPanel actionsControlSouthPanel = new HorizontalPanel();
		actionsControlSouthPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		actionsControlSouthPanel.setSpacing(5);
		actionsControlSouthPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		actionsPanel.add(actionsControlSouthPanel, DockPanel.SOUTH);
		actionsPanel.setCellHorizontalAlignment(actionsControlSouthPanel, HasHorizontalAlignment.ALIGN_RIGHT);
		actionsControlSouthPanel.setWidth("100%");

		Button hideActionsButton = new Button("Hide actions panel button");
		hideActionsButton.setText("Hide");
		hideActionsButton.setWidth("67px");
		hideActionsButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				actionsMenuTabBar.selectTab(-1);
				actionsPanel.setVisible(false);
			}
		});
		actionsControlSouthPanel.add(hideActionsButton);

		dataPanel = new VerticalPanel();
		dataPanel.getElement().getStyle().setTop(5.0, Unit.PX);
		dataPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		dataPanel.setSpacing(1);
		containerPanel.add(dataPanel);
		dataPanel.setWidth("100%");
	}

	public DecoratedTabBar getActionsMenuTabBar() {
		return actionsMenuTabBar;
	}

	public HasWidgets getActionDataCenterPanel() {
		return actionDataCenterPanel;
	}

	public HasWidgets getDataPanel() {
		return dataPanel;
	}

	public String getTabCaption() {
		return tabCaption;
	}

	public void setTabCaption(String tabCaption) {
		this.tabCaption = tabCaption;
	}

	public String getTabIdentifier() {
		return tabIdentifier;
	}

	public void setTabIdentifier(String tabIdentifier) {
		this.tabIdentifier = tabIdentifier;
	}
}
